package cn.maxwell.pet.controller;

import cn.maxwell.basic.util.BusinessException;
import cn.maxwell.basic.util.JsonResult;
import cn.maxwell.basic.util.LoginContext;
import cn.maxwell.basic.util.PageList;
import cn.maxwell.pet.domain.SearchMasterMsg;
import cn.maxwell.pet.dto.AcceptDto;
import cn.maxwell.pet.query.SearchMasterMsgQuery;
import cn.maxwell.pet.service.ISearchMasterMsgService;
import cn.maxwell.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/searchMasterMsg")
@Api(value = "宠物寻主消息寻主消息的API",description="宠物寻主消息相关的CRUD功能")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping //get请求
    @ApiOperation(value = "查询所有")
    public List<SearchMasterMsg> loadAll() {
        return searchMasterMsgService.loadAll();
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}") //get请求，携带id参数
    @ApiOperation(value = "通过ID查询")
    //swagger注解：paramType="path"：解析path路径携带的参数；dataType="Long"：指定参数类型
    @ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "path", dataType = "Long")
    public SearchMasterMsg loadById(@PathVariable("id") Long id) {//@PathVariable：获取path路径携带的参数
        return searchMasterMsgService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除操作")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 添加或修改
     *
     * @param searchMasterMsg
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "添加或修改操作")
    public JsonResult save(@RequestBody SearchMasterMsg searchMasterMsg) {//@RequestBody:在请求体中获取参数
        try {
            if (searchMasterMsg.getId() == null) {
                searchMasterMsgService.add(searchMasterMsg);
            } else {
                searchMasterMsgService.update(searchMasterMsg);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     *
     * @param searchMasterMsgQuery
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "分页查询")
    public PageList<SearchMasterMsg> query(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery) {
        return searchMasterMsgService.queryPage(searchMasterMsgQuery);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public JsonResult patchDelete(@RequestBody List<Long> ids) { //List<Long>:要删除的id的集合
        try {
            searchMasterMsgService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping("/publish")
    @ApiOperation(value = "寻主消息")
    public JsonResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            //获取登录信息
            Logininfo logininfo = (Logininfo) LoginContext.getLoginUser(request);
            Long logininfo_id = logininfo.getId();
            searchMasterMsgService.publish(searchMasterMsg,logininfo_id);
            return new JsonResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙，请稍后再试！");
        }
    }

    //================消息列表====================//
// 待审核或驳回：查询待审核或驳回的寻主消息 - 平台管理员才能查询 - 可以通过按钮权限限制，这里不做判断
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query){
        query.setState(0);
        return searchMasterMsgService.queryPage(query);
    }
    // 寻主池：查询审核通过还没有分配店铺或者被拒单的寻主消息 - state=1，ShopId为null
/*@PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> toSearchMasterPool(@RequestBody SearchMasterMsgQuery query){
        query.setState(1);
        //由于寻主池查询条件和待处理查询条件冲突，但是查询的结构不一样，所以寻主池业务需要重新写
        //查询state=1，shopId is null的寻主消息
        return searchMasterMsgService.queryPagePool(query);
    }*/
// 已完成：查询已完成的寻主消息 - 平台管理员才能查询 - 可以通过按钮权限限制，这里不做判断
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query){
        query.setState(2);
        return searchMasterMsgService.queryPage(query);
    }
    // 用户寻主列表：用于登录之后再个人中心查询自己所有状态的寻主消息
    @PostMapping("/userSearch")
    public PageList<SearchMasterMsg> userSearchMasterMsg(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return searchMasterMsgService.userSearchMasterMsg(query,request);
    }

    // 待处理：查询审核通过并已经分配了店铺的寻主消息 - state=1，ShopId不为null
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return searchMasterMsgService.toHandle(query,request);
    }

    // 拒单
    @GetMapping("/reject/{id}")
    public JsonResult reject(@PathVariable("id")Long id){
        try {
            searchMasterMsgService.reject(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("拒单失败！");
        }
    }


//    // 接单
//    @PostMapping("/accept")
//    public JsonResult accept(@RequestBody Map<String,Object> params){
//        try {
//            searchMasterMsgService.accept(params);
//            return JsonResult.me();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return JsonResult.me().setMsg("接单失败！"+e.getMessage());
//        }
//    }
    /**
     * 接单
     * @param
     * @return
     */
    @PostMapping("/accept")
    @ApiOperation(value = "接单" )
    public JsonResult accept(@RequestBody AcceptDto dto, HttpServletRequest request){
        try {
            Logininfo logininfo = (Logininfo) LoginContext.getLoginUser(request);
            searchMasterMsgService.accept(dto,logininfo.getId());
            return JsonResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

}